﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Arkanoid
{
    class Bola : Object 
    {
       
        public float VelocidadeX = 0.0f;
        public float VelocidadeY = 0.0f;

        public int Pontuacao = 0;

        int ControlaTecla = 0;
        Random random = new Random();
        SoundEffect soundEffect;

        public Bola(GameWindow janela, Texture2D textura, Vector2 position, SoundEffect soundEffect)
            :base(janela,textura,position)  
        {
            this.size = textura.Bounds;
            this.position = new Vector2((janela.ClientBounds.Width / 2 - this.size.Width / 2), 360);
            this.soundEffect = soundEffect;  
        }

        public override void Update(){}

        public void Update(GameTime gametime, GameWindow janela, Pallete player1,List<Keys> teclado) 
        {
            this.LancarBolaGame(teclado, gametime); 
            ManterNaTela(janela, player1);

            this.size.X = (int) position.X;
            this.size.Y = (int)position.Y;
        }

        private void LancarBolaGame(List<Keys> teclado, GameTime gametime)
        {
            foreach (Keys tecla in teclado)
            {
                if (ControlaTecla == 0)
                {
                    if (Keyboard.GetState().IsKeyDown(tecla) && tecla == Keys.L)
                    {
                        VelocidadeX = 4.0f;
                        VelocidadeY = -4.0f;
                    }
                }
            } 
        }

        private void ManterNaTela(GameWindow janela, Pallete player1)
        {
            if (this.position.Y < 0 || this.position.Y < 5) 
            {
                VelocidadeY  *= -1;
                soundEffect.Play();  
            }

            if (this.position.Y > 395) 
            {
                VelocidadeY *= -1;
                player1.Life = player1.Life - 1;  
                this.position = new Vector2((janela.ClientBounds.Width / 2 - this.size.Width / 2), 360);
            }

            if (this.position.X > janela.ClientBounds.Width - this.texture.Width || this.position.X < 0)
            {
                VelocidadeX *= -1;
                soundEffect.Play();
            }

            if (this.GetBounding().Intersects(player1.GetBounding()))
            {
                VelocidadeY *= -1;
                soundEffect.Play();
            }

            this.position.X += VelocidadeX;
            this.position.Y += VelocidadeY;
        }

        public BoundingSphere GetBounding()
        {
            return new BoundingSphere(new Vector3(this.position.X + this.size.Width / 2,
                    this.position.Y + this.size.Height / 2, 0.0f), this.size.Width / 2);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);      
        }
    }
}
